package hell.satan.droidbdf.survey;

import android.app.Activity;
import android.content.Context;

import java.util.HashMap;
import java.util.Map;

/**
 * 数据上报的代理(例如友盟等)。
 * Proxy for statistics (Such as Flurry).
 *
 * @author Cerberus
 */
@SuppressWarnings("unused")
public class SurveyAgent {
    private static Map<String, ISurvey> sSurveys = new HashMap<>();
    private static Map<String, Boolean> sEnabled = new HashMap<>();

    /**
     * 注册一个数据上报代理
     * Register a survey
     *
     * @param id     数据上报代理的标识
     *               Identifier for each survey
     * @param survey 上报代理的实例
     *               Instance of survey
     */
    public static void registerSurvey(String id, ISurvey survey) {
        sSurveys.put(id, survey);
    }

    /**
     * 反注册一个数据上报代理
     * Unregister a survey
     *
     * @param id 数据上报代理的标识
     *           Identifier for each survey
     */
    public static void unregisterSurvey(String id) {
        sSurveys.remove(id);
    }

    /**
     * 启用或禁用一个上报代理
     * Enable or disable a survey
     *
     * @param id      数据上报代理的标识
     *                Identifier for each survey
     * @param enabled true表示启用，false表示禁用
     *                true for enable, false for disable
     */
    public static void setEnabled(String id, boolean enabled) {
        sEnabled.put(id, enabled);
    }

    /**
     * 上报Activity的onResume事件，仅上报给已启用的代理
     * Reported onResume events of an activity, only reported to enabled surveys
     *
     * @param activity 触发onResume事件的Activity
     *                 Activity of who triggered OnResume event
     */
    public static void onResume(Activity activity) {
        for (String id : sSurveys.keySet()) {
            if (sEnabled.get(id)) sSurveys.get(id).onResume(activity);
        }
    }

    /**
     * 上报Activity的onPause事件，仅上报给已启用的代理
     * Reported onPause events of an activity, only reported to enabled surveys
     *
     * @param activity 触发onPause事件的Activity
     *                 Activity of who triggered onPause event
     */
    public static void onPause(Activity activity) {
        for (String id : sSurveys.keySet()) {
            if (sEnabled.get(id)) sSurveys.get(id).onPause(activity);
        }
    }

    /**
     * 上报一个无附加数据的普通事件，仅上报给已启用的代理
     * Reported an ordinary event without additional data, only reported to enabled surveys
     *
     * @param event 事件名称
     *              Name of event
     */
    public static void onEvent(Context context, String event) {
        for (String id : sSurveys.keySet()) {
            if (sEnabled.get(id)) sSurveys.get(id).onEvent(context, event);
        }
    }

    /**
     * 上报一个有简单数据的普通事件，仅上报给已启用的代理
     * Reported an ordinary event with simple data, only reported to enabled surveys
     *
     * @param event 事件名称
     *              Name of event
     * @param value 附加简单数据
     *              Additional simple data
     */
    public static void onEvent(Context context, String event, String value) {
        for (String id : sSurveys.keySet()) {
            if (sEnabled.get(id)) sSurveys.get(id).onEvent(context, event, value);
        }
    }

    /**
     * 上报一个有复杂附加数据的普通事件，仅上报给已启用的代理
     * Reported an ordinary event with complex data, only reported to enabled surveys
     *
     * @param event  事件名称
     *               Name of event
     * @param extras 附加复杂数据
     *               Additional complex data
     */
    public static void onEvent(Context context, String event, Map<String, String> extras) {
        for (String id : sSurveys.keySet()) {
            if (sEnabled.get(id)) sSurveys.get(id).onEvent(context, event, extras);
        }
    }

    /**
     * 上报一个事件的开始，仅上报给已启用的代理
     * Reported the beginning of an event, only reported to enabled surveys
     *
     * @param event 事件名称
     *              Name of event
     */
    public static void onEventBegin(Context context, String event) {
        for (String id : sSurveys.keySet()) {
            if (sEnabled.get(id)) sSurveys.get(id).onEventBegin(context, event);
        }
    }

    /**
     * 上报一个事件的结束，仅上报给已启用的代理
     * Reported the ending of an event, only reported to enabled surveys
     *
     * @param event 事件名称
     *              Name of event
     */
    public static void onEventEnd(Context context, String event) {
        for (String id : sSurveys.keySet()) {
            if (sEnabled.get(id)) sSurveys.get(id).onEventEnd(context, event);
        }
    }

    /**
     * 上报Activity的onResume事件，仅上报给ids指定的代理，无论它是否已启用
     * Reported onResume events of an activity, only reported to surveys whitch id is in ids,
     * no matter if it is enabled
     *
     * @param activity 触发onResume事件的Activity
     *                 Activity of who triggered OnResume event
     * @param ids      接收数据的代理的标识
     *                 Identifiers of surveys who can report
     */
    public static void onResume(Activity activity, String[] ids) {
        for (String id : ids) {
            if (sSurveys.get(id) != null) sSurveys.get(id).onResume(activity);
        }
    }

    /**
     * 上报Activity的onPause事件，仅上报给ids指定的代理，无论它是否已启用
     * Reported onPause events of an activity, only reported to surveys whitch id is in ids,
     * no matter if it is enabled
     *
     * @param activity 触发onPause事件的Activity
     *                 Activity of who triggered onPause event
     * @param ids      接收数据的代理的标识
     *                 Identifiers of surveys who can report
     */
    public static void onPause(Activity activity, String[] ids) {
        for (String id : ids) {
            if (sSurveys.get(id) != null) sSurveys.get(id).onPause(activity);
        }
    }

    /**
     * 上报一个无附加数据的普通事件，仅上报给ids指定的代理，无论它是否已启用
     * Reported an ordinary event without additional data, only reported to surveys whitch id is in ids,
     * no matter if it is enabled
     *
     * @param ids   接收数据的代理的标识
     *              Identifiers of surveys who can report
     * @param event 事件名称
     *              Name of event
     */
    public static void onEvent(Context context, String[] ids, String event) {
        for (String id : ids) {
            if (sSurveys.get(id) != null) sSurveys.get(id).onEvent(context, event);
        }
    }

    /**
     * 上报一个有简单数据的普通事件，仅上报给ids指定的代理，无论它是否已启用
     * Reported an ordinary event with simple data, only reported to surveys whitch id is in ids,
     * no matter if it is enabled
     *
     * @param ids   接收数据的代理的标识
     *              Identifiers of surveys who can report
     * @param event 事件名称
     *              Name of event
     * @param value 附加简单数据
     *              Additional simple data
     */
    public static void onEvent(Context context, String[] ids, String event, String value) {
        for (String id : ids) {
            if (sSurveys.get(id) != null) sSurveys.get(id).onEvent(context, event, value);
        }
    }

    /**
     * 上报一个有复杂附加数据的普通事件，仅上报给ids指定的代理，无论它是否已启用
     * Reported an ordinary event with complex data, only reported to surveys whitch id is in ids,
     * no matter if it is enabled
     *
     * @param ids    接收数据的代理的标识
     *               Identifiers of surveys who can report
     * @param event  事件名称
     *               Name of event
     * @param extras 附加复杂数据
     *               Additional complex data
     */
    public static void onEvent(Context context, String[] ids, String event, Map<String, String> extras) {
        for (String id : ids) {
            if (sSurveys.get(id) != null) sSurveys.get(id).onEvent(context, event, extras);
        }
    }

    /**
     * 上报一个事件的开始，仅上报给ids指定的代理，无论它是否已启用
     * Reported the beginning of an event, only reported to surveys whitch id is in ids,
     * no matter if it is enabled
     *
     * @param ids   接收数据的代理的标识
     *              Identifiers of surveys who can report
     * @param event 事件名称
     *              Name of event
     */
    public static void onEventBegin(Context context, String[] ids, String event) {
        for (String id : ids) {
            if (sSurveys.get(id) != null) sSurveys.get(id).onEventBegin(context, event);
        }
    }

    /**
     * 上报一个事件的结束，仅上报给ids指定的代理，无论它是否已启用
     * Reported the ending of an event, only reported to surveys whitch id is in ids,
     * no matter if it is enabled
     *
     * @param ids   接收数据的代理的标识
     *              Identifiers of surveys who can report
     * @param event 事件名称
     *              Name of event
     */
    public static void onEventEnd(Context context, String[] ids, String event) {
        for (String id : ids) {
            if (sSurveys.get(id) != null) sSurveys.get(id).onEventEnd(context, event);
        }
    }

    /**
     * 上报一个无附加数据的普通事件，不同的代理可使用不同的事件名
     * Reported an ordinary event without additional data, different survey can use different event names
     *
     * @param idEvents 偶数位元素为接收数据的代理标识，奇数位元素为事件的名称
     *                 Even element is the identifier of survey, and the odd element is event name
     */
    public static void onEvent(Context context, String... idEvents) {
        for (int i = 0; i < idEvents.length / 2; i++) {
            String id = idEvents[2 * i];
            String event = idEvents[2 * i + 1];
            if (sSurveys.get(id) != null) sSurveys.get(id).onEventEnd(context, event);
        }
    }
}
